package com.skplanet.rade.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.skplanet.rade.dao.StoryTagDAO;
import com.skplanet.rade.dao.TagDAO;
import com.skplanet.rade.domain.Story;
import com.skplanet.rade.domain.StoryTag;
import com.skplanet.rade.domain.Tag;
import com.skplanet.rade.domain.User;
import com.skplanet.rade.domain.UserTag;
import com.skplanet.rade.util.StringUtils;

@Service( value="storyTagService" )
@Transactional
public class StoryTagService {

	@Autowired
	private TagService tagService;
	
	@Autowired
	private StoryTagDAO storyTagDAO;

	@Autowired
	private PasswordEncoder passwordEncoder;
	
	
	@Transactional
	public void update(StoryTag storyTag){
		storyTagDAO.update(storyTag);
	}
	
	@Transactional
	public void addStoryTag(StoryTag storyTag) {
		storyTagDAO.save( storyTag );
	}
	
	@Transactional(readOnly = true)
	public StoryTag select( String stringTagId ) {
		StoryTag storyTag = storyTagDAO.select( stringTagId );
		return storyTag;
	}

	@Transactional(readOnly = true)
	public boolean keywordExists(String keyword) {
		return storyTagDAO.rowExists("keyword", keyword);
	}
	
//	@Transactional
//	public StoryTag storyTagExists(Story story, String keyword) {
//		List<StoryTag> stList = story.getStoryTags();
//		List<StoryTag> tsList = storyTagDAO.findListByObject("keyword", keyword);
//		StoryTag storyTag = null;
//		if(stList != null) {
//			stList.retainAll(tsList);
//			if(stList.size() > 0) {
//				storyTag = stList.get(0);
//			}
//		}
//
//		if (storyTag == null) {
//			storyTag = new StoryTag();
//			storyTag.setKeyword(keyword);
//		}
//		
//		return storyTag;
//	}
	
	@Transactional(readOnly = true)
	public StoryTag getStoryTagFromKeyword(String keyword) {
		StoryTag storyTag;
		if((StoryTag) storyTagDAO.findUniqByObject("keyword", keyword) == null) {
			storyTag = new StoryTag();
			storyTag.setKeyword(keyword);
		} else {
			storyTag = (StoryTag) storyTagDAO.findUniqByObject("keyword", keyword);			
		}
		
		return storyTag;
	}
}
